define(function (require, exports, module) {

   // var PDFAnnot = WebPDF.PDFData.PDFAnnot;

    require("../../PDFData/AnnotFactory.js")
    var annotFactory = WebPDF.AnnotFactory;

    WebPDF.Tools.TOOL_NAME_TEXTANNOT = "TextAnnot Tool";

    WebPDF.Tools.TextAnnotToolHandler = function (annotPlugin) {
        var _readerApp = null,
        //    _language = null,
    		_baseAnnotPlugin = annotPlugin,
    		_isProcessing = false,
    		_self = this;
        var MKA_POPUP_WIDTH = WebPDF.PDFData.MKA_POPUPWIDTH;
        var MKA_POPUP_HEIGHT = WebPDF.PDFData.MKA_POPUPHEIGHT;
        var TEXT_ANNOT_DEFAULT_WIDTH = 20;
        var TEXT_ANNOT_DEFAULT_HEIGHT = 20;
        var DEFAULT_OPACITY = 1;

        this.onInit = function (readerApp) {
            _readerApp = readerApp;
            //_language = _readerApp.getLanguage();
        };

        this.onDestroy = function () {
        };

        this.getName = function () {
            return WebPDF.Tools.TOOL_NAME_TEXTANNOT;
        };


        this.onActivate = function () {
			var docView = _readerApp.getMainView().getDocView();
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11){
                jqDocViewContainer.addClass("fwr-note-cursor-ie");
            }
			else  {
                jqDocViewContainer.addClass("fwr-note-cursor");
            }
        };

        this.onDeactivate = function () {var docView = _readerApp.getMainView().getDocView();
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
			if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11) {
                jqDocViewContainer.removeClass("fwr-note-cursor-ie");
            }
            else {
                jqDocViewContainer.removeClass("fwr-note-cursor");
            }
        };

        this.isEnabled = function () {
            var currentDocument = _readerApp.getPDFDoc();
            if (currentDocument) {
                if (currentDocument.getDocType() != WebPDF.PDFDocType.XFA)
                    return true;
            }
            /*
            * if (!(pDoc->GetDocumentType() == DOCTYPE_DYNIMIC_XFA))
            return pDoc->GetPermissions(FPDFPERM_ANNOT_FORM) ; 
            * */
            return true;
        };

        this.isProcessing = function () {
            return _isProcessing;
        };

        this.onLButtonDown = function (event) {
            
            if ($(event.target).hasClass("fwr-annot-scaleImg")){                
                   this.signScale = true;
                   this.direction = $(event.target).attr("direction");
                   this.curMouseX = event.pageX;
                   this.curMouseY = event.pageY;
                   this.curObject = $(event.target).parent();
                   return true;
            }
            var pageView = null;
            var bAnnot = null;
            var docView = _readerApp.getMainView().getDocView();
            if($(event.target).hasClass("fwr-annot")){
              bAnnot =  event.target.className;
            }
            
            

            if (bAnnot)
			{
				var pageIndex = parseInt(event.target.getAttribute("page-index"));
                var annotName = event.target.getAttribute("annot-name");
				pageView = docView.getPageView(pageIndex);
                var pdfPage = pageView.getPDFPage();
                var annot = pdfPage.getAnnotByName(annotName);
				if (annot) {
					if (annot.getType() === "Markup" && annot.getSubType() === "Text")
					{
						return false;
					}
				}
			}

            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            if (!WebPDF.Common.containsNode(jqDocViewContainer.get(0), event.target, true)) {
                return false;
            }

            //褰撳墠榧犳爣鏄惁鍦ㄩ〉闈腑
            var visablePageRange = docView.getVisiblePageRange();
            var pageView = null;
            var pageViewRect = null;
            for (var i = visablePageRange.begin; i <= visablePageRange.end; i++) {
                var tmpPageView = docView.getPageView(i);
                var pageOffsetHtmlDoc = $("#" + tmpPageView.getPageViewContainerID()).offset();
                var pageViewWidth = tmpPageView.getPageViewWidth();
                var pageViewHeight = tmpPageView.getPageViewHeight();
                pageViewRect = new WebPDF.PDFRect(pageOffsetHtmlDoc.left, pageOffsetHtmlDoc.top,
						pageOffsetHtmlDoc.left + pageViewWidth, pageOffsetHtmlDoc.top + pageViewHeight);
                WebPDF.RectUtils.normalize(pageViewRect);
                if (WebPDF.RectUtils.ptInRect(pageViewRect, event.pageX, event.pageY)) {
                    pageView = tmpPageView;
                    break;
                }
            }

            if (pageView != null) {
				WebPDF.Common.preventDefaults(event, true);
                _self.createAnnot(pageView, event.pageX - pageViewRect.left, event.pageY - pageViewRect.bottom);
                docView.setModified(true);
                //_baseAnnotPlugin.setSaveCommentBtnState(true);
            }

            _isProcessing = true;
            return true;
        };

        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐氓录鹿猫碌路盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonUp = function (event) {
            _isProcessing = false;
            return true;
        };

        this.createAnnot = function (pageView, pageXofPageView, pageYofPageView) {
            var dt = new Date();
            var time = dt.valueOf() + dt.getTimezoneOffset() * 60000;
            var timestamp = (Math.floor(time / 1000)).toString();
            var annots_json = {
                c: "",
                ca: DEFAULT_OPACITY,
                cd: timestamp,
                cl: "#f78f20",
                f: WebPDF.PDFData.AnnotFlag.ANNOTFLAG_PRINT,
                md: timestamp,
                n: timestamp,
                pop: null,
               // subj: _language.Annot.AnnotSubjNote,
                subj: "Note",
                subty: "Text",
                tit: _readerApp.getUserName(),
                ty: "Markup",
                Quad: [],
                txtn: 'Comment',
                rc: null
            };

            var pDFPage = pageView.getPDFPage();
            var pdfPageWidth = pDFPage.getPageWidth();
            var pdfPageHeight = pDFPage.getPageHeight();
            var curPointPDF = new WebPDF.PDFPoint(pageXofPageView, pageYofPageView);
            curPointPDF = pageView.docPtToPDF(curPointPDF);
			curPointPDF.x = curPointPDF.x;
			curPointPDF.y = curPointPDF.y;
            var halfWidth = TEXT_ANNOT_DEFAULT_WIDTH / 2;
            var halfHeight = TEXT_ANNOT_DEFAULT_HEIGHT / 2;
            var pdfrect = new WebPDF.PDFRect(curPointPDF.x - halfWidth, curPointPDF.y + halfHeight, curPointPDF.x + halfWidth,
        			 curPointPDF.y - halfHeight);

            if (pdfrect.left < 0) {
                WebPDF.RectUtils.offset(pdfrect, 0 - pdfrect.left, 0);
            }
            if (pdfrect.bottom < 0) {
                WebPDF.RectUtils.offset(pdfrect, 0, 0 - pdfrect.bottom);
            }
            if (pdfrect.right > pdfPageWidth) {
                WebPDF.RectUtils.offset(pdfrect, pdfPageWidth - pdfrect.right, 0);
            }
            if (pdfrect.top > pdfPageHeight) {
                WebPDF.RectUtils.offset(pdfrect, 0, pdfPageHeight - pdfrect.top);
            }

            annots_json.rc = [pdfrect.left, pdfrect.top, pdfrect.right, pdfrect.bottom];

            var popupRect = new WebPDF.PDFRect(0, 0, 0, 0);
            popupRect.top = pdfrect.top;
			var pixelsPerPt = _readerApp.getPixelsPerPoint();
			var viewScale = pageView.getDocView().getScale();
            popupRect.left = (pageView.getPageViewWidth()-MKA_POPUP_WIDTH*pixelsPerPt)/(viewScale*pixelsPerPt);
            popupRect.right = popupRect.left + MKA_POPUP_WIDTH;
            popupRect.bottom = popupRect.top - MKA_POPUP_HEIGHT;
			if(popupRect.bottom < 0)
			{
				WebPDF.RectUtils.offset(popupRect, 0, -popupRect.bottom);
			}

            annots_json.pop =
        	 {
        	     f: WebPDF.PDFData.AnnotFlag.ANNOTFLAG_PRINT,
        	     md: timestamp,
        	     n: timestamp + "_popup",
        	     op: 1,
        	     rc: [popupRect.left, popupRect.top, popupRect.right, popupRect.bottom]
        	 };

            var annot = annotFactory.createAnnot(annots_json, false);
            pDFPage.getAnnotJsonData().push(annots_json);
            pDFPage.addAnnot(annot);
            pageView.setModified(true);
            var pageAnnotsDiv = _baseAnnotPlugin.getAnnotHandlerMgr().getUIManager().getPageAnnotsContainerID(pageView);
            var jqPageAnnotsDiv = $("#" + pageAnnotsDiv);
            var annotName = annot.getAnnotName();
            var newAnnotHtml = _baseAnnotPlugin.getAnnotHandlerMgr().getUIManager().createAAnnotHtmlContent(pageView, annot, annotName);
            jqPageAnnotsDiv.append(newAnnotHtml);
           // _baseAnnotPlugin.getAnnotHandlerMgr().BindPopupContextmenuEvent();
			var jqPagesContainerDiv = $("#" + pageView.getDocView().getDocViewContainerID());
            var contentPanelHeight = jqPagesContainerDiv.height(); // $('#mainContainer').height();
            var contentPanelWidth = jqPagesContainerDiv.width();
             var contentPanelOffset = jqPagesContainerDiv.offset();
			var rcAnnotVisableRect = _baseAnnotPlugin.getAnnotHandlerMgr().getAnnotVisibleRect(pageView, contentPanelOffset, contentPanelWidth, contentPanelHeight);
            
            
           // var annotHandler = annotHandlerMgr.getAnnotHandlerByAnnot(annot);
			_baseAnnotPlugin.getAnnotHandlerMgr().updateAnnotsPosition(pageView, rcAnnotVisableRect,annot);
			_baseAnnotPlugin.getAnnotHandlerMgr().setFocusAnnot(pageView,annot);
            //annotHandler.TypewriterAnnot_Edit(pPageView, annotName);
        };      
      


        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐氓锟脚捗モ�禄盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonDblClk = function (event) {
            return false;
        };
        /**
        * @description 盲赂禄莽陋鈥斆ぢ解�莽拧鈥灻┞悸犆β犫�莽搂禄氓艩篓盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseMove = function (event) {
            if(this.signScale){
                var offsetX = event.pageX - this.curMouseX;
                var offsetY = event.pageY - this.curMouseY;
                this.curMouseX = event.pageX;
                this.curMouseY = event.pageY;               
                this.resizeAnnot(this.curObject,offsetX,offsetY,this.direction);
                return true;
     }

            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ε掆�盲赂鈥姑ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonDown = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ヂ悸姑德访ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonUp = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ワ拷艗氓鈥÷幻ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonDblClk = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶β慌∶铰β慌∶ヅ犅ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseWheel = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶幻库�盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�莽搂禄氓艩篓氓藛掳氓庐茠氓颅锟矫モ�茠莽麓聽忙鈥斅睹ぢ古该ぢ寂∶γワ拷鈥樏ッぢ衡�盲禄露茂录鈥懊ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseOver = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶幻モ�潞盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�莽搂禄氓艩篓氓藛掳氓庐茠氓颅锟矫モ�茠莽麓聽忙鈥斅睹ぢ古该ぢ寂∶γワ拷鈥樏ッぢ衡�盲禄露茂录鈥懊ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseOut = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶库�氓鈥βッぢ衡�盲禄露茂录藛茅录聽忙聽鈥∶ワ拷陋忙艙鈥懊库�氓鈥βッッモ�茠莽麓聽氓艗潞氓鸥鸥忙鈥帮拷盲录拧猫搂娄氓锟解�猫炉楼盲潞鈥姑ぢ宦睹尖�氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseEnter = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶β幻ヂ尖偓盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�氓锟铰ε撯�莽娄禄氓录鈧ッモ�茠莽麓聽氓艗潞氓鸥鸥忙鈥帮拷盲录拧猫搂娄氓锟解�猫炉楼盲潞鈥姑ぢ宦睹尖�氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseLeave = function (event) {
            return false;
        };
        this.onKeyDown = function (event) {
            return false;
        };
        this.onKeyUp = function (event) {
            return false;
        };
        this.onChar = function (event) {
            return false;
        };
    };

    return WebPDF.Event.TextAnnotToolHandler;
});